package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import com.itheima.utils.fileNameUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){

        System.out.println("clearImg执行");

        Set<String> sdiff = jedisPool.getResource().sdiff(
                RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);


        if (sdiff!=null&&sdiff.size()>0){
            for (String imgName : sdiff) {
                //删除tomcat上的图片
               del(imgName);
                //从redis集合中删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
                System.out.println("清理垃圾图片:"+imgName);
            }
        }
    }


    public static void del(String imgName){
            String path="http://localhost:8080/fileServer/upload/";
            //创建客户端
             Client client = Client.create();

             //链接服务器,跨服务器删除
             client.resource(path+imgName).delete();
    }


//    public void clearImg(){
//        Set<String> sdiff = jedisPool.getResource().sdiff(
//                RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
//
//
//        if (sdiff!=null){
//            for (String imgName : sdiff) {
//                //删除七牛云上的图片
//                QiniuUtils.deleteFileFromQiniu(imgName);
//                //从redis集合中删除
//                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
//            }
//        }
//    }

}
